# -*- coding: utf-8 -*-
# TabPanel.py

import wx
import os
import shutil
import zipfile
# import effectsmanager
currentFilePath = __file__
parentPath = os.path.dirname(currentFilePath)
oriPath = os.path.dirname(parentPath)
from listDir import listAllDir

class TabPanel(wx.Panel):
    '''
    This will be th first notebook tab
    '''
    #----------------------------------------
    def __init__(self,*args,**kwargs):
        wx.Panel.__init__(self,*args,**kwargs)

        self.stickerPaths = listAllDir(oriPath+"/Product/")
        print(self.stickerPaths)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, u"选择当前要操作的帖纸")
        hbox1.Add(label)
        vbox.Add(hbox1,0,wx.ALIGN_CENTER_HORIZONTAL|wx.TOP,30)

        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        ch1=wx.ComboBox(self,style=wx.CB_READONLY,size=(120,29))
        self.__combo = ch1
        ch1.SetItems(self.stickerPaths)
        if len(self.stickerPaths)>0:
            ch1.SetSelection(0)
        hbox2.Add(ch1)
        vbox.Add(hbox2,0,wx.ALIGN_CENTER_HORIZONTAL|wx.TOP,20)

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        btn = wx.Button(self,-1,"删除",size=(50,20))
        self.__deleteBtn = btn
        btn.Bind(wx.EVT_BUTTON, lambda evt,mark="":self.OnClicked(evt,""))
        hbox3.Add(btn)
        vbox.Add(hbox3,0,wx.ALIGN_CENTER_HORIZONTAL|wx.TOP,20)

        hbox4 = wx.BoxSizer(wx.HORIZONTAL)
        btn2 = wx.Button(self,-1,"同步到手机",size=(100,40))
        btn2.Bind(wx.EVT_BUTTON, lambda evt,mark="":self.OnClicked2(evt,""))
        hbox4.Add(btn2,0,wx.RIGHT,10)
        btn3 = wx.Button(self,-1,"打包成zip",size=(100,40))
        btn3.Bind(wx.EVT_BUTTON, lambda evt,mark="":self.OnClicked3(evt,""))
        hbox4.Add(btn3)
        vbox.Add(hbox4,0,wx.ALIGN_CENTER_HORIZONTAL|wx.TOP,20)

        self.SetSizer(vbox)
    def OnClicked(self, event,parm):
        if len(self.stickerPaths)<1:
            print("贴纸目录为空")
            msg = wx.MessageDialog(None, u'当前没有已经生成的贴纸，无法进行当前的操作', u'小提示', wx.YES_NO | wx.ICON_QUESTION)
            msg.ShowModal()
            return
        msgDialog = wx.MessageDialog(None, u'删除后就无法再次生成', u'确定要删除贴纸内容？', wx.YES_NO | wx.ICON_QUESTION)
        if msgDialog.ShowModal() == wx.ID_NO:
            print('点击了NO')
        else:
            if len(self.stickerPaths)>0:
                currentIndex = self.__combo.GetCurrentSelection()
                #删除
                deleName = self.stickerPaths[currentIndex]
                if os.path.exists(oriPath+"/Product/"+deleName):
                    shutil.rmtree(oriPath+"/Product/"+deleName)
                #print(deleName)
                del self.stickerPaths[currentIndex]
                
            if len(self.stickerPaths)>0:
                self.__combo.SetItems(self.stickerPaths)
                self.__combo.SetSelection(0)
            else:
                self.__combo.SetItems([])

    def OnClicked2(self, event,parm): #同步到手机
        if len(self.stickerPaths)<1:
            print("贴纸目录为空")
            msg = wx.MessageDialog(None, u'当前没有已经生成的贴纸，无法进行当前的操作', u'小提示', wx.YES_NO | wx.ICON_QUESTION)
            msg.ShowModal()
            return  
        androidFileManager = oriPath+"/androidfilemanager.sh "
        currentIndex = self.__combo.GetCurrentSelection()
        stickerName = self.stickerPaths[currentIndex]
        os.system(androidFileManager + "-push " + "Product/"+stickerName+"/" +stickerName)
    def OnClicked3(self, event,parm): #打包
        if len(self.stickerPaths)<1:
            print("贴纸目录为空")
            msg = wx.MessageDialog(None, u'当前没有已经生成的贴纸，无法进行当前的操作', u'小提示', wx.YES_NO | wx.ICON_QUESTION)
            msg.ShowModal()
            return
        currentIndex = self.__combo.GetCurrentSelection()
        stickerName = self.stickerPaths[currentIndex]
        stickerPath = oriPath+"/Product/"+stickerName+"/"
        print(stickerPath)
        self.make_zip(stickerPath+stickerName+"/"+"script",stickerPath+stickerName+"/"+"script.zip")    
    def make_zip(self,source_dir, output_filename):
        zipf = zipfile.ZipFile(output_filename, 'w')
        pre_len = len(os.path.dirname(source_dir))
        for parent, dirnames, filenames in os.walk(source_dir):
            for filename in filenames:
                pathfile = os.path.join(parent, filename)
                arcname = pathfile[pre_len:].strip(os.path.sep)   #相对路径
                zipf.write(pathfile, arcname)
        zipf.close()       